<template>
	<view class="baseForm">
		<!-- 选择加工单 -->
		<view class="tab-bar">
			<u-tabs :current="activeTab" :list="tabList" :scrollable="false" :activeStyle="{
					color: '#3895fc',
					fontWeight: 'bold',
				}" lineWidth="50px" lineHeight="2px" lineColor="#007aff" @change="changeTab"></u-tabs>
		</view>

		<view class="" v-if="activeTab == 0">
			<view class="searchScan_div">
				<scan-bar ref="scanBarRef" placeholder="请扫描包装条码" @code-scanned="getScanResult"
					:key="datekey"></scan-bar>
				<u-button @click="feedHadle" style="width: 60px; height: 33px;font-size: 13px;" type="primary">
					投料
				</u-button>
			</view>
			<!-- 当前作业 -->
			<view v-if="feedList.length > 0">
				<!-- 列表 -->
				<u-list ref="tableScroll" class="list" :height="listHeight" lowerThreshold="50px">
					<u-list-item v-for="(item, index) in feedList" :key="item.id">
						<view class="card">
							<view class="flex record-title">
								<text class="line1">
									{{ item.materialName }}
								</text>
							</view>
							<view class="flex flex-col justify-between flex-1 h-full record-content-body">
								<view class="record-content">
									<view class="record-item">
										<text>物料条码:{{ item.materialBarcode }}</text>
									</view>
									<view class="record-item">
										<text>批次号:{{ item.materialBatch }}</text>
										<text>数量: {{ item.materialQuantity }}</text>
									</view>
									<view class="record-item">
										<text>
											投料人: {{ item.feedMaterialName }}
										</text>
										<text>
											投料时间: {{ item.feedMaterialTime }}
										</text>
									</view>
									<u-line style="margin: 5px 0 2px" color="#dadbde"></u-line>
									<view style="
											display: flex;
											justify-content: space-between;
										">
										<view class=""></view>
										<view @click="selectItem(item, index)" style="
												border: 1px solid #313f62;
												padding: 2px 15px 2px 15px;
												border-radius: 15px;
												color: #313f62;
												height: 20px;
												line-height: 20px;
												font-size: 12px;
											">
											取消上料
										</view>
									</view>
								</view>
							</view>
						</view>
					</u-list-item>
				</u-list>
			</view>
			<view class="flex justify-center" v-show="tableLoading">
				<u-loading-icon color="#000a7b"></u-loading-icon>
			</view>
			<!-- 空数据 -->
			<view v-show="tableList.length === 0 && !tableLoading">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
			</view>
		</view>
		<view class="" v-if="activeTab == 1">
			<!-- 列表 -->
			<view class="" v-if="workOrderList[0]?.id">
				<u-list ref="tableScrolls" class="list" :height="listHeight" lowerThreshold="50px">
					<u-list-item v-for="(item, index) in workOrders" :key="item.id">
						<view class="card">
							<view class="flex record-title">
								<text class="line1">{{ item.materialName }}~{{ item.materialCode }}</text>
								<text>
									{{ item.processesClassificationName || "无" }}
								</text>
							</view>
							<view class="flex flex-col justify-between flex-1 h-full record-content-body">
								<view class="record-content">
									<view class="record-item">
										<text>计划开始时间</text>
										<text>
											{{ item.planBeginTime || "无" }}
										</text>
									</view>
									<view class="record-item">
										<text>计划结束时间</text>
										<text class="">
											<text>{{ item.planEndTime || "无" }}</text>
										</text>
									</view>
									<view class="record-item">
										<text>计划数量: {{ item.planQuantity || 0}}</text>
										<text>完成数量:{{ item.finishQuantity || 0}}</text>
									</view>
									<view class="record-item">
										<text>炉批次: {{ item.plannedBatch }}</text>
										<text>状态: {{ item.formStatusName }}</text>
									</view>
									<view class="record-item">
										<view class="">工单号:{{ item.processingOrderNumber }}</view>
									</view>
									<!-- <u-line
											v-if="workOrderList[0]?.id == item.id"
											style="margin: 5px 0 2px"
											color="#dadbde"></u-line> -->
									<!-- <view class="record-item">
											<view class=""></view>
											<view class=""></view>
											<view
												v-if="
													workOrderList[0].id == item.id
												"
												style="
													padding: 5px 15px 5px 15px;
													border-radius: 5px;
													color: #fff;
													font-size: 12px;
													background-color: #0379ff;
												">
												已选择
											</view>
										</view> -->
								</view>
							</view>
						</view>
					</u-list-item>
				</u-list>
			</view>

			<!-- 空数据 -->
			<view v-show="!workOrderList[0]?.id">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
			</view>
		</view>
		<!-- 工序选择器 -->
		<u-modal :show="showImport" ref="uModal" :content="`是否确取消上料？`" confirmText="确认" showCancelButton
			@cancel="cancelImport" @confirm="confirmImport"></u-modal>
	</view>
</template>

<script lang="ts" setup>
	import { ref, computed, toRaw } from "vue";
	import { onShow, onPullDownRefresh, onLoad } from "@dcloudio/uni-app";
	import { http } from "@/utils/http";
	import ScanBar from '@/components/ScanBarH5/ScanBarH5.vue';
	import { PageResult, TabInfo } from "@/types/type";
	import { getSecondTime } from "@/utils/common";
	import { useDictionaryStore } from "@/stores/useDictionaryStore";
	import { useProduceStore } from "@/stores/useProduceStore";

	// 实时获取
	const workOrderList = computed(() =>
		JSON.parse(useProduceStore().workOrderList)
	);

	// 待完成数据
	const tableList = ref([]);
	const tableLoading = ref(false);
	const current = ref(0);
	const totalPage = ref(0);
	const total = ref(0);
	//列表高度
	const listHeight = ref("");

	onLoad(() => {
		// #ifdef H5
		listHeight.value = "calc(100vh - 88px)";
		// #endif

		// #ifdef APP-PLUS
		listHeight.value = "calc(100vh - 48px)";
		// #endif
		// loadmore();
		if (workOrderList?.value[0]?.id) {
		  getFeedList()
		  getWorkOrders()
		}
	});

	onPullDownRefresh(() => {
		// searchList();
		// uni.stopPullDownRefresh();
	});

	async function loadmore(type ?: string) {
		try {
			current.value += 1;
			tableLoading.value = true;
			const result : PageResult<any> = await http({
				method: "POST",
				url: "/eam/equAssetInfo/queryByPage",
				data: {
					current: current.value,
					size: 20,
					data: {
						searchAssetInfo: value1.value === 1 ? searchText.value : "",
						locationId: value1.value === 2 ? positionId.value : "",
					},
				},
			}).finally(() => {
				tableLoading.value = false;
			});
			const list = result.data;
			for (let i = 0; i < list.length; i++) {
				tableList.value.push(list[i]);
			}
			totalPage.value = result.page;
			total.value = result.total;
			type === "refresh" && uni.stopPullDownRefresh();
		} catch (e) {
			console.log(e);
		}
	}
	const scrolltolower = () => {
		totalPage.value > current.value && loadmore();
	};

	// 点击扫码
	const searchText = ref("");

	// 列表搜索
	function searchList() {
		// current.value = 0;
		// tableList.value = [];
		// loadmore("refresh");
	}

	function clearList() {
		console.log("点击");
		searchText.value = "";
		// searchList();
	}

	// --------------------------------------------------------------------- 切换按钮
	const activeTab = ref(0);
	const tabList = [
		{ name: "投料", value: 0 },
		{ name: "当前工单", value: 1 },
	];
	function changeTab(val : TabInfo) {
		console.log(val);
		activeTab.value = val.index;
	}
	// --------------------------------------------------------------------- 投料 ---扫描
	// 重置表单信息 & 光标自动blur
	// 投料列表
	const feedList: any = ref([]);
	const scanBarRef = ref();
	const searchContent = ref("");
	const datekey = ref(new Date());
	function resetFields() {
		searchContent.value = "";
		scanBarRef.value.reset();
		datekey.value = new Date();
	}
	// 获取扫码结果
	function getScanResult(result) {
		if (result) {
			if (workOrderList.value[0]?.id) {
				searchContent.value = result;
				// 请求一遍然后在添加
				feedScan(result);
				resetFields();
			} else {
				resetFields();
				uni.showToast({
					title: "请选择加工单！",
					icon: "none",
				});
			}
		}
	}
	// 点击投料按钮
	const feedHadle = () => {
		if (workOrderList.value[0]?.id) {
			if (scanBarRef.value.scanCode) {
				// 请求一遍然后在添加
				feedScan(scanBarRef.value.scanCode);
				resetFields();
			} else {
				uni.showToast({
					title: "请扫描包装条码！",
					icon: "none",
				});
			}
		} else {
			resetFields();
			uni.showToast({
				title: "请选择加工单！",
				icon: "none",
			});
		}
	};
	// ---------------------------------------------------------------------  投料列表

	// 获取投料列表
	const getFeedList = async () => {
		await http({
			method: "POST",
			url: "/mes/mesproducingstockrecord/getList",
			data: {
				workingFormId: workOrderList.value[0]?.id,
				isFeedMaterial: 1,
			},
		}).then((res) => {
			feedList.value = res.data;
		});
	};

	// 上料扫描的物料
	const feedScan = async (code) => {
		await http({
			method: "POST",
			url: "/mes/mesproducingstockrecord/toFeeding",
			data: {
				materialBarcode: code,
				workingFormId: workOrderList.value[0]?.id,
				processesClassification: workOrderList.value[0]?.processesClassification
			},
		}).then((res : any) => {
			if (res.code == 200) {
				uni.showToast({
					title: "投料成功！",
					icon: "none",
				});
				getFeedList();
			}
		}).finally(()=>{
			getFeedList();
		});
	};
	// 弹窗展示
	const showImport = ref(false);
	const indexFeed = ref(null);
	const itemFeed = ref();
	function cancelImport() {
		showImport.value = false;
	}
	async function confirmImport() {
		showImport.value = false;
		await http({
			method: "POST",
			url: "/mes/mesproducingstockrecord/cancelMaterial",
			data: itemFeed.value,
		}).then((res : any) => {
			if (res.code == 200) {
				uni.showToast({
					title: "取消成功",
				});
				getFeedList();
			}
		}).finally(()=>{
			getFeedList();
		});

	}
	// 点击取消上料

	const selectItem = (item, index) => {
		showImport.value = true;
		indexFeed.value = index;
		itemFeed.value = item;
	};
	
	// -------------------当前工单
	const workOrders = ref()
	// 工单列表
	const getWorkOrders = async () => {
		await http({
			method: "GET",
			url: `/mes/mesplanworkingform/get/${workOrderList.value[0]?.id}`,
		}).then((res) => {
			let item : any = res.data
			if (Number(item.formStatus) && !isNaN(Number(item.formStatus))) {
				item.formStatusName = useDictionaryStore().filterDictName("ProduceOrderState", item.formStatus)
			}
			if (Number(item.processesClassification) && !isNaN(Number(item.processesClassification))) {
				item.processesClassificationName = useDictionaryStore().filterDictName("ProcessesClass", item.processesClassification)
			}
			workOrders.value = [item]
		});
	};
</script>

<style lang="scss" scoped>
	uni-page-body {
		height: 100%;
		background-color: #fbfbff;
	}

	.search {
		:deep(.u-search__content) {
			box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
		}
	}

	.baseForm {
		background-color: #fbfbff;

		.tab-bar {
			padding: 0 20px;
		}

		.upload-scan {
			padding: $uni-page-padding;
			padding-top: 5px;
			padding-bottom: 5px;
			display: flex;
			align-items: center;

			// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
			.u-icon {
				margin-left: 8px;
			}
		}

		.u-line {
			padding: 10px 0;
		}
	}

	.list {
		.card {
			margin: $uni-page-padding;
			margin-top: 5px;
			margin-bottom: 4px;
			border-radius: 7px;
			padding: 12px 12px 0px 12px;
			transition: all 300ms ease;
			border: 1px solid #efefef;
			background-color: #fff;

			.card-flex {
				display: flex;
				align-items: flex-start;
			}

			.right {
				flex: 1;
			}

			.title {
				font-size: 14px;
				line-height: 40px;
				height: 40px;
				margin-top: 8px;
				font-weight: bold;
				color: #101010;
				border-top: 1px solid #e5ebff;
			}

			.uni-btn {
				float: right;
				font-size: 12px;
				height: 24px;
				line-height: 24px;
				padding: 0 15px;
				margin-top: 7px;
				font-weight: 400;
			}

			.des {
				font-size: 12px;
				line-height: 20px;
				margin-bottom: 3px;
			}

			.left {
				flex-basis: calc(50% + 10px);
			}
		}
	}

	.record-title {
		display: flex;
		justify-content: space-between;
		margin: 2px 0 2px 0;
		color: $primary-color;

		uni-text:nth-child(1) {
			font-weight: bold;
			font-size: 15px;
		}

		uni-text:nth-child(2) {
			font-weight: bold;
			font-size: 15px;
			min-width: 60px;
			text-align: right;
		}
	}

	.record-item {
		margin: 7px 0 !important;
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}

	.record-item1 {
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}

	.record-content .record-item> :first-child {
		min-width: 70px !important;
	}

	:deep(.input-box) {
		// width: calc(100% - 56px);
		width: 100%;
		// border: 0.5px solid #dadbde !important;
		border-radius: 100px;
		height: 32px;
		line-height: 32px;
		box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
		background-color: #fff;

		.u-input {
			pointer-events: none;
			height: 32px;
			line-height: 32px;
			padding-left: 10px !important;
		}

		.uni-input-placeholder {
			color: rgb(144, 147, 153) !important;
			font-size: 14px;
		}

		.u-icon__icon {
			font-size: 22px !important;
			line-height: 22px;
			font-weight: normal;
			top: 0px;
			color: rgb(144, 147, 153) !important;
		}

		image {
			padding: 6px 6px;
			width: 20px;
			height: 20px;
		}
	}

	:deep(.search-btn) {
		width: 32px;
		height: 34px;
		margin-left: 4px;
		border-radius: 4px;
		background-color: transparent;
		border: none;

		// .u-icon__icon {
		// 	margin-left: 5px;
		// 	color: rgb(96, 98, 102) !important;
		// 	font-size: 26px !important;
		// }
		.u-button__text {
			color: rgb(96, 98, 102) !important;
		}
	}

	:deep(.searchButton) {
		color: rgb(41, 121, 255);
		font-size: 14px;
		width: 52px !important;
		min-width: 52px !important;
		display: flex;
		justify-content: flex-start;

		.u-icon {
			margin-left: 2px !important;
			font-size: 14px;

			.u-icon__icon {
				font-size: 14px !important;
				line-height: 14px !important;
			}
		}
	}

	:deep(.u-dropdown) {
		flex: none !important;
		width: 52px;

		.u-cell-group__wrapper {
			// border: 1px solid rgb(214, 215, 217);
			// border-radius: 4px;
			border-right: 1px solid rgb(214, 215, 217);
			border-left: 1px solid rgb(214, 215, 217);
		}

		// .u-line { display: none;}
		.u-cell__body {
			padding: 8px 10px !important;
			// border: 1px solid #8f8e94;
		}

		.u-dropdown__content__mask {
			background-color: transparent;
		}
	}

	// :deep(.u-drawdown-item__content) {
	// 	top: 88px !important;
	// 	.common-search {
	// 		display: flex;
	// 		justify-content: flex-start;
	// 		margin: 0 15px;
	// 		padding: 13px 5px;
	// 		border-bottom: 1px solid rgb(214, 215, 217);
	// 	}
	// }

	// :deep(.dis-none){
	// 		.u-dropdown__content{
	// 			display: none;
	// 		}
	// 	}
</style>